﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;

namespace ZhonTai.Admin.Domain.Patrol
{
    public static class DataExtension
    {
        public static void FillKVToMe<T>(this T tdata, KeyValuePair<string, object> kp) where T : PatrolTaskNodeItemEntity
        {
            var tc = Type.GetTypeCode(kp.Value.GetType());

            switch (tc)
            {
                case TypeCode.Boolean:
                    tdata.UniDataType = UniDataType.Boolean;
                    tdata.Value_Boolean = (bool)kp.Value;
                    break;
                case TypeCode.Single:
                    tdata.UniDataType = UniDataType.Double;
                    tdata.Value_Double = double.Parse(kp.Value.ToString(), NumberStyles.Float);
                    break;
                case TypeCode.Double:
                case TypeCode.Decimal:
                    tdata.UniDataType = UniDataType.Double;
                    tdata.Value_Double = (double)kp.Value;
                    break;

                case TypeCode.Int16:
                case TypeCode.Int32:
                case TypeCode.Int64:
                case TypeCode.UInt16:
                case TypeCode.UInt32:
                case TypeCode.UInt64:
                case TypeCode.Byte:
                case TypeCode.SByte:
                    tdata.UniDataType = UniDataType.Long;
                    tdata.Value_Long = (Int64)Convert.ChangeType(kp.Value, TypeCode.Int64);
                    break;
                case TypeCode.String:
                case TypeCode.Char:
                    tdata.UniDataType = UniDataType.String;
                    tdata.Value_String = (string)kp.Value;
                    break;
                case TypeCode.DateTime:
                    tdata.UniDataType = UniDataType.DateTime;
                    tdata.Value_DateTime = ((DateTime)kp.Value);
                    break;
                case TypeCode.DBNull:
                case TypeCode.Empty:
                    break;
                case TypeCode.Object:
                default:
                    if (kp.Value.GetType() == typeof(JsonElement))
                    {
                        var kvx = kp.Value as JsonElement?;
                        if (kvx.HasValue)
                        {
                            switch (kvx.Value.ValueKind)
                            {
                                case JsonValueKind.Undefined:
                                case JsonValueKind.Object:
                                    break;
                                case JsonValueKind.String:
                                    tdata.UniDataType = UniDataType.String;
                                    tdata.Value_String = kvx.Value.GetString();
                                    break;
                                case JsonValueKind.Number:
                                    tdata.UniDataType = UniDataType.Double;
                                    tdata.Value_Double = kvx.Value.GetDouble();
                                    break;
                                case JsonValueKind.True:
                                case JsonValueKind.False:
                                    tdata.UniDataType = UniDataType.Boolean;
                                    tdata.Value_Boolean = kvx.Value.GetBoolean();
                                    break;
                                case JsonValueKind.Null:
                                    break;
                                default:
                                    break;
                            }
                        }
                    }
                    else
                    {
                        tdata.UniDataType = UniDataType.Json;
                        tdata.Value_Json = Newtonsoft.Json.JsonConvert.SerializeObject(kp.Value);
                    }
                    break;
            }
        }
    }
}
